// src/api/invoice.ts
import http from './http'

// 进项发票类型定义
export interface IncomingInvoice {
  id: string | number
  invoiceNumber: string
  invoiceCode: string
  invoiceTypeCode: string
  isRedInvoice: 'Y' | 'N'
  invoiceDate: string
  supplierId: string
  sellerTaxNumber: string
  sellerName: string
  buyerTaxNumber: string
  buyerName: string
  totalAmount: number
  taxAmount: number
  totalTaxAmount: number
  issuer: string
  taxBureauCode: string
  taxBureauName: string
  remark: string
  status?: string
  createdAt?: string
  updatedAt?: string
}

// 销项发票类型定义
export interface OutgoingInvoice {
  id: string | number
  invoiceNumber: string
  invoiceCode: string
  invoiceTypeCode: string
  isRedInvoice: 'Y' | 'N'
  invoiceDate: string
  customerId: string
  sellerTaxNumber: string
  sellerName: string
  buyerTaxNumber: string
  buyerName: string
  totalAmount: number
  taxAmount: number
  totalTaxAmount: number
  issuer: string
  taxBureauCode: string
  taxBureauName: string
  remark: string
  status?: string
  createdAt?: string
  updatedAt?: string
}

// 分页查询参数
export interface PageQuery {
  page: number
  pageSize: number
  invoiceNumber?: string
  sellerName?: string
  buyerName?: string
  startDate?: string
  endDate?: string
  sortField?: string
  sortOrder?: string
}

// 分页响应结果
export interface PageResult<T> {
  items: T[]
  total: number
  page: number
  pageSize: number
}

// API响应结构
interface ApiResponse<T> {
  code: number
  message: string
  data: T
}

// 获取进项发票列表
export async function getIncomingInvoices(query: PageQuery): Promise<PageResult<IncomingInvoice>> {
  const params = new URLSearchParams()
  Object.entries(query).forEach(([key, value]) => {
    if (value !== undefined && value !== null && value !== '') {
      params.append(key, value.toString())
    }
  })
  
  const response: ApiResponse<PageResult<IncomingInvoice>> = await http.get(`/incoming-invoices?${params.toString()}`)
  return response.data
}

// 获取进项发票详情
export async function getIncomingInvoiceById(id: string | number): Promise<IncomingInvoice> {
  const response: ApiResponse<IncomingInvoice> = await http.get(`/incoming-invoices/${id}`)
  return response.data
}

// 创建进项发票
export async function createIncomingInvoice(data: Partial<IncomingInvoice>): Promise<IncomingInvoice> {
  const response: ApiResponse<IncomingInvoice> = await http.post('/incoming-invoices', data)
  return response.data
}

// 更新进项发票
export async function updateIncomingInvoice(id: string | number, data: Partial<IncomingInvoice>): Promise<IncomingInvoice> {
  const response: ApiResponse<IncomingInvoice> = await http.put(`/incoming-invoices/${id}`, data)
  return response.data
}

// 删除进项发票
export async function deleteIncomingInvoice(id: string | number): Promise<void> {
  await http.delete(`/incoming-invoices/${id}`)
}

// 导入进项发票
export async function importIncomingInvoices(file: File): Promise<any> {
  const formData = new FormData()
  formData.append('file', file)
  const response: ApiResponse<any> = await http.post('/incoming-invoices/import', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
  return response.data
}

// 导出进项发票
export async function exportIncomingInvoices(query: PageQuery): Promise<Blob> {
  const params = new URLSearchParams()
  Object.entries(query).forEach(([key, value]) => {
    if (value !== undefined && value !== null && value !== '') {
      params.append(key, value.toString())
    }
  })
  
  const response: Blob = await http.get(`/incoming-invoices/export?${params.toString()}`, {
    responseType: 'blob'
  })
  return response
}

// 获取销项发票列表
export async function getOutgoingInvoices(query: PageQuery): Promise<PageResult<OutgoingInvoice>> {
  const params = new URLSearchParams()
  Object.entries(query).forEach(([key, value]) => {
    if (value !== undefined && value !== null && value !== '') {
      params.append(key, value.toString())
    }
  })
  
  const response: ApiResponse<PageResult<OutgoingInvoice>> = await http.get(`/outgoing-invoices?${params.toString()}`)
  return response.data
}

// 获取销项发票详情
export async function getOutgoingInvoiceById(id: string | number): Promise<OutgoingInvoice> {
  const response: ApiResponse<OutgoingInvoice> = await http.get(`/outgoing-invoices/${id}`)
  return response.data
}

// 创建销项发票
export async function createOutgoingInvoice(data: Partial<OutgoingInvoice>): Promise<OutgoingInvoice> {
  const response: ApiResponse<OutgoingInvoice> = await http.post('/outgoing-invoices', data)
  return response.data
}

// 更新销项发票
export async function updateOutgoingInvoice(id: string | number, data: Partial<OutgoingInvoice>): Promise<OutgoingInvoice> {
  const response: ApiResponse<OutgoingInvoice> = await http.put(`/outgoing-invoices/${id}`, data)
  return response.data
}

// 删除销项发票
export async function deleteOutgoingInvoice(id: string | number): Promise<void> {
  await http.delete(`/outgoing-invoices/${id}`)
}

// 导入销项发票
export async function importOutgoingInvoices(file: File): Promise<any> {
  const formData = new FormData()
  formData.append('file', file)
  const response: ApiResponse<any> = await http.post('/outgoing-invoices/import', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
  return response.data
}

// 导出销项发票
export async function exportOutgoingInvoices(query: PageQuery): Promise<Blob> {
  const params = new URLSearchParams()
  Object.entries(query).forEach(([key, value]) => {
    if (value !== undefined && value !== null && value !== '') {
      params.append(key, value.toString())
    }
  })
  
  const response: Blob = await http.get(`/outgoing-invoices/export?${params.toString()}`, {
    responseType: 'blob'
  })
  return response
}